// this sets the background color of the master UIView (when there are no windows/tab groups on it)

(function () { 
Lhs.ui.CreateMapWindow = function () {
	var pathimage=Ti.App.Properties.getString('pathimage');
	var win = Titanium.UI.createWindow({
	    id: 'MapWindow',
	    title: 'ĐỊA ĐIỂM',
	    fullscreen: true,
	    backgroundImage:pathimage+'bg.png',
	    navBarHidden:true,
	});
	
	var egc = new globals.EventGarbageCollection(win);
	egc.addEventListener('back_'+win.id, function () {
		Lhs.navGroup.close(win, {
	         animated: true
	    });
		
	});
	egc.addEventListener('getLocation', function () {
		//require('settings').createAlertDialog2(Globals.title,'Bạn có muốn xem chỉ dẩn đường đi ? ',GetLocation());
		
		var confirm = Titanium.UI.createAlertDialog({ 
					            title: Globals.title, 
					            message:'Bạn có muốn xem chỉ dẩn đường đi ? ', 
					            buttonNames: [Globals.btOk,Globals.btCancel]
					    });
		 confirm.addEventListener('click', function(e) {
	       //Clicked cancel, first check is for iphone, second for android
	       if (e.cancel === e.index || e.cancel === true) {
	          return;
	       }
	       switch (e.index) {
	          case 0: {
	          			 GetLocation();
					  }
	          break;
	 
	          //This will never be reached, if you specified cancel for index 1
	          case 1: Titanium.API.info('Clicked button 1 (NO)');
	          break;
	 
	          default:
	          break;
	      }
	 
	    });
		confirm.show();   
	
	
	});
	
	//HEADER ON WINDOWS
	var main_header=require('controls/header').Createheader({
		  	idWinParent:win.id,
		  	height:Ti.App.Properties.getInt('height_Header'),//44,
		  	title:'ĐỊA ĐIỂM',
		  	isLocation:1,
	})
	win.add(main_header);
			
	
	var	db=[
			{title:'Công Ty TNHH Truyền Thông Tùng Việt',subtitle:'151 Nguyễn Đình Chính,Phường 11, Phú Nhuận, Hồ Chí Minh',latitude:10.7949,longitude:106.6741},
			
	      ]
	var annotations=[]
	for(var i = 0; i < db.length; i++) {
		var mountainView = Titanium.Map.createAnnotation({
		    latitude:db[i].latitude,
		    longitude:db[i].longitude,
		    title:db[i].title,
		    subtitle:db[i].subtitle,
		    pincolor:Titanium.Map.ANNOTATION_RED,
		    animate:true,
		    leftButton:pathimage+ 'company.png',
		    pinImage:pathimage+ 'company.png',
		    myid:i
		});
		annotations[i]=mountainView
	}
	var cur_longitude;
	var cur_latitude ;
	Titanium.Geolocation.getCurrentPosition(function(e)
	{
	    if (e.error)
	    {
	        alert('HFL cannot get your current location');
	        return;
	    }
	 
	    cur_longitude = e.coords.longitude;
	    cur_latitude = e.coords.latitude;
	  
	 
	})
	var mapview = Titanium.Map.createView({
		top:main_header.height,
	    mapType: Titanium.Map.STANDARD_TYPE,
	    region: {latitude:cur_latitude, longitude:cur_longitude, 
	           latitudeDelta:0.01, longitudeDelta:0.01},
	    animate:true,
	    regionFit:true,
	    userLocation:true,
	    latitudeDelta:10,
	    longitudeDelta:10,
	    regionFit:true,
	    annotations:annotations,//[mountainView]
	  
	});
	
	win.add(mapview);
	// Handle click events on any annotations on this map.
	mapview.addEventListener('click', function(evt) {
	
	   
	});
	
	
	
	var loadGPSInformation = function(e) {
			
		var datajson=e.datajson;
		//Ti.API.info(datajson)
		
		var points=[];
		var i = 0;
		var j = 1;
		for(i = 0; i < datajson.length; i++) {
			
			var latitude=datajson[i].latitude;
			var longitude=datajson[i].longitude;
			points[i]= {latitude:latitude, longitude:longitude}
		}
		
		//Ti.API.info(points)
		
		
		mapview.addRoute({
			
		    name: 'myroute',
		    width: 4,
		    color: '#f00',
		    points: points
		});
	
	
	};
	
	
	 var GetLocation = function() { 
	 	var from=db[0].latitude+','+db[0].longitude;
	 	var to=cur_latitude+','+cur_longitude;//'10.7806,106.6441';
	 	require('windows/MapAPI').loadGPSInformation(from,to,loadGPSInformation);
	 }
	
	
	return win;
       
    };
})();